package com.wanxin.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.wanxin.matcher.RetyLimitHashedCredentialsMatcher;
import com.wanxin.relam.MyRelam;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.io.ResourceUtils;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;


@Configuration
public class ShiroConfig {
    //声明自定义的MyBealm的属性
    @Autowired
    private MyRelam myRelam;

    //声明自定义的匹配器
    @Autowired
    private RetyLimitHashedCredentialsMatcher retyLimitHashedCredentialsMatcher;

//    @Autowired
//    private MyRelam2 myRelam2;


    /* 编写SecurityManager的bean配置方法
     *
     * SpringBoot项目启动时会自动加载配置类的资源，
     * 并将bean的方法配置的资源完成初始化设置然后放到Spring容器中
     * 相当于我们之前的bean标签的配置
     * */
    @Bean
    protected DefaultWebSecurityManager defaultWebSecurityManager() {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        //设置登入开启md5加密
        retyLimitHashedCredentialsMatcher.setHashAlgorithmName("md5");
        retyLimitHashedCredentialsMatcher.setHashIterations(5);
        myRelam.setCredentialsMatcher(retyLimitHashedCredentialsMatcher);

//        //设置组合的Realm策略
//        ModularRealmAuthenticator modularRealmAuthenticator=new ModularRealmAuthenticator();
//        modularRealmAuthenticator.setAuthenticationStrategy(new AllSuccessfulStrategy());
//        defaultWebSecurityManager.setAuthenticator(modularRealmAuthenticator);
//        //创建集合存储自定义的Relam
//        List<Realm> relamList=new ArrayList<>();
//        relamList.add(myRelam);
//        relamList.add(myRelam2);
//        //将自定义的realm集成到DefaultWebSecurityManager
//        defaultWebSecurityManager.setRealms(relamList);

        //设置shiro的remember me的功能
        defaultWebSecurityManager.setRememberMeManager(rememberMeManager());
        //设置EhCache的缓存
        defaultWebSecurityManager.setCacheManager(getEhCacheManager());

        defaultWebSecurityManager.setRealm(myRelam);
        return defaultWebSecurityManager;
    }


    /*
     * 设置RememberMeManager对象
     * */
    public CookieRememberMeManager rememberMeManager() {
        //创建CookieRememberMeManager对象
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        //1.设置Cookie的参数（cookie的有效期）
        cookieRememberMeManager.setCookie(rememberMeCookie());
        //2.对Cookie进行加密
        cookieRememberMeManager.setCipherKey("1234567898765432".getBytes());
        //返回
        return cookieRememberMeManager;
    }


    /*
     * 设置Cookie的信息
     * */
    public SimpleCookie rememberMeCookie() {
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        cookie.setPath("/");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(30 * 24 * 60 * 60);
        return cookie;
    }


    /*
     * 配置shiro整合Ehcache的标识的cacheManager的bean
     * */
    @Bean
    public EhCacheManager getEhCacheManager() {
        //1.创建Shiro的cacheManager对象
        EhCacheManager cacheManager = new EhCacheManager();
        //2.Ehcache官方的CacheManager对象
        //获取Ehcache的配置文件的流对象
        InputStream is = null;
        try {
            is = ResourceUtils.getInputStreamForPath("classpath:ehcache/ehcache-shiro.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        net.sf.ehcache.CacheManager cm = new net.sf.ehcache.CacheManager(is);
        //3.将Ehcache的CacheManager对象存储到Shiro的CacheManager对象中
        cacheManager.setCacheManager(cm);
        return cacheManager;

    }


    /*
     * 配置Shiro的内置过滤器
     * */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        //放行登入页面请求路径
        definition.addPathDefinition("/login", "anon");
        definition.addPathDefinition("/userLogin", "anon");
        definition.addPathDefinition("/logout", "logout");
        definition.addPathDefinition("/**", "user");
        return definition;
    }


    /*
     * 配置页面的shiro标识的解析bean
     * */
    @Bean
    public ShiroDialect shiroDialect() {
        return new ShiroDialect();
    }


}
